package com.wxyzz.cem.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wxyzz.cem.exception.BalanceApplyServiceException;
import com.wxyzz.cem.pojo.BalanceApply;
import com.wxyzz.cem.mapper.BalanceApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxyzz.cem.pojo.ClassInfo;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.pojo.enums.BalanceApplyStatus;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * <p>
 * 班费申请 服务实现类
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@Service
public class BalanceApplyService extends ServiceImpl<BalanceApplyMapper, BalanceApply> implements IService<BalanceApply> {
    
    public Page<BalanceApply> pageData(Integer page, Integer size, Integer classId) {
        Page<BalanceApply> page1 = new Page<>(page, size);
        LambdaQueryWrapper<BalanceApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BalanceApply::getClassId, classId);
        queryWrapper.orderByDesc(BalanceApply::getApplyTime);
        Page<BalanceApply> balanceApplyPage = this.page(page1, queryWrapper);
        balanceApplyPage.getRecords().forEach(balanceApply -> {
            User user = Db.getById(balanceApply.getUserId(), User.class);
            balanceApply.setUser(user);
        });
        return balanceApplyPage;
    }
    
    public void apply(BalanceApply balanceApply) {
        balanceApply.setStatus(BalanceApplyStatus.WAITING);
        // 保留两位小数
        Float formatAmount = Float.parseFloat(new DecimalFormat("0.00").format(balanceApply.getAmount()));
        balanceApply.setAmount(formatAmount);
        boolean success = this.save(balanceApply);
        if (!success) {
            throw new BalanceApplyServiceException();
        }
    }
    
    public void accept(BalanceApply balanceApply) {
        BalanceApply oldBalanceApply = this.getById(balanceApply.getId());
        if (oldBalanceApply.getStatus() != BalanceApplyStatus.WAITING) {
            throw new BalanceApplyServiceException("无需审核！");
        }
        
        ClassInfo classInfo = Db.getById(oldBalanceApply.getClassId(), ClassInfo.class);
        if (classInfo.getBalance() < oldBalanceApply.getAmount()) {
            throw new BalanceApplyServiceException("班费不足");
        }
        
        oldBalanceApply.setBeforeClassBalance(classInfo.getBalance());
        oldBalanceApply.setStatus(BalanceApplyStatus.AGREE);
        oldBalanceApply.setLoanImageName(balanceApply.getLoanImageName());
        oldBalanceApply.setAuditTime(new Timestamp(System.currentTimeMillis()));
        
        classInfo.setBalance(classInfo.getBalance() - oldBalanceApply.getAmount());
        
        Db.updateById(classInfo);
        this.updateById(oldBalanceApply);
    }
    
    public void refuse(BalanceApply balanceApply) {
        BalanceApply oldBalanceApply = this.getById(balanceApply.getId());
        if (oldBalanceApply.getStatus() != BalanceApplyStatus.WAITING) {
            throw new BalanceApplyServiceException("无需审核！");
        }
        oldBalanceApply.setStatus(BalanceApplyStatus.REFUSE);
        oldBalanceApply.setAuditTime(new Timestamp(System.currentTimeMillis()));
        this.updateById(oldBalanceApply);
    }
    
    public void cancel(BalanceApply balanceApply) {
        BalanceApply oldBalanceApply = this.getById(balanceApply.getId());
        if (oldBalanceApply.getStatus() != BalanceApplyStatus.WAITING) {
            throw new BalanceApplyServiceException("无需撤回！");
        }
        if (!Objects.equals(oldBalanceApply.getUserId(), balanceApply.getUserId())) {
            throw new BalanceApplyServiceException("无权撤回！");
        }
        oldBalanceApply.setStatus(BalanceApplyStatus.REVOKE);
        this.updateById(oldBalanceApply);
    }
}
